<template>
	<div style="margin: 20px;">
        <div>
            <Row style="margin-bottom: 25px;">
                <Col span="8">请选择小区名称：
                    <Select v-model="residenceId" style="width:200px">
                        <Option v-for="item in allBuild" :value="item.id" :key="item.id">{{ item.projectName }}</Option>
                    </Select>
                </Col>
                <Col span="8">请输入楼栋名称：
                    <Input v-model="buildingId" style="width: 204px" placeholder="请输入楼栋名称"/>
                </Col>
                <Col span="8"><Button type="primary" shape="circle" icon="ios-search" @click="search()">搜索</Button></Col>
            </Row>
        </div>            
        <div>
            <ul>
                <li>
                    <Button type="primary" icon="plus-round" @click="openNewModal()">新建</Button>
                    <Button type="success" icon="wrench" @click="openModifyModal()">修改</Button>
                    <Button type="error" icon="trash-a" @click="del()">删除</Button>
                </li>
                <li>
                    <div style="padding: 10px 0;">
                    	<Table border :columns="columns1" :data="data1" :height="400" @on-selection-change="s=>{change(s)}" @on-row-dblclick="s=>{dblclick(s)}"></Table>
                    </div> 
                </li>
                <li>
                    <div style="text-align: right;">
                        <Page :total="total" :page-size="pageInfo.pageSize" show-elevator show-total @on-change="e=>{pageSearch(e)}"></Page>
                    </div>  
                </li>
            </ul>
        </div>
        <!--添加modal-->  
        <Modal :mask-closable="false" :visible.sync="newModal" :loading = "loading" v-model="newModal" width="600" title="新建" @on-ok="newOk('buildingAdd')">
            <Form ref="buildingAdd" :model="buildingAdd" :rules="ruleNew" :label-width="80" >
                <Row>
                    <Col span="12">
                        <Form-item label="所属小区:" prop="projectId">
                            <Select v-model="buildingAdd.projectId" style="width:204px">
                                <Option v-for="item in allBuild" :value="item.id" :key="item.id">{{ item.projectName }}</Option>
                            </Select>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="楼栋名称:" prop="buildingName">
                            <Input v-model="buildingAdd.buildingName" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                        <Form-item label="楼层数:" prop="floorTotal">
                            <Input v-model="buildingAdd.floorTotal" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="户数:" prop="houseTotal">
                            <Input v-model="buildingAdd.houseTotal" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                    <Form-item label="建筑年代:" prop="architecturalAge">
                        <Input v-model="buildingAdd.architecturalAge" style="width: 204px"/>
                    </Form-item>
                    </Col>
                    <Col span="12">
                    <Form-item label="产权特质:" prop="propertyRights">
                        <Input v-model="buildingAdd.propertyRights" style="width: 204px"/>
                    </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                    <Form-item label="房屋类型:" prop="buildingLevel">
                        <Input v-model="buildingAdd.buildingLevel" style="width: 204px"/>
                    </Form-item>
                    </Col>
                    <Col span="12">
                    <Form-item label="有无电梯:" prop="hasLeft">
                        <!-- <Input v-model="buildingAdd.hasLeft" style="width: 204px"/> -->
                        <RadioGroup v-model="buildingAdd.hasLeft" style="width:204px">
                            <Radio label="0">
                                <span>有</span>
                            </Radio>
                            <Radio label="1">
                                <span>无</span>
                            </Radio>
                        </RadioGroup>
                    </Form-item>
                    </Col>
                </Row>
            </Form>
        </Modal>
        <!--修改modal-->
        <Modal :mask-closable="false" :visible.sync="modifyModal" :loading = "loading" v-model="modifyModal" width="600" title="新建" @on-ok="modifyOk('buildingModify')">
            <Form ref="buildingModify" :model="buildingModify" :rules="ruleNew" :label-width="80" >
                <Row>
                    <Col span="12">
                        <Form-item label="所属小区:" prop="projectId">
                            <Select v-model="buildingModify.projectId" style="width:204px">
                                <Option v-for="item in allBuild" :value="item.id" :key="item.id">{{ item.projectName }}</Option>
                            </Select>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="楼栋名称:" prop="buildingName">
                            <Input v-model="buildingModify.buildingName" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                        <Form-item label="楼层数:" prop="floorTotal">
                            <Input v-model="buildingModify.floorTotal" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="户数:" prop="houseTotal">
                            <Input v-model="buildingModify.houseTotal" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                    <Form-item label="建筑年代:" prop="architecturalAge">
                        <Input v-model="buildingModify.architecturalAge" style="width: 204px"/>
                    </Form-item>
                    </Col>
                    <Col span="12">
                    <Form-item label="产权特质:" prop="propertyRights">
                        <Input v-model="buildingModify.propertyRights" style="width: 204px"/>
                    </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                    <Form-item label="房屋类型:" prop="buildingLevel">
                        <Input v-model="buildingModify.buildingLevel" style="width: 204px"/>
                    </Form-item>
                    </Col>
                    <Col span="12">
                    <Form-item label="有无电梯:" prop="hasLeft">
                        <RadioGroup v-model="buildingModify.hasLeft" style="width:204px">
                            <Radio label="0">
                                <span>有</span>
                            </Radio>
                            <Radio label="1">
                                <span>无</span>
                            </Radio>
                        </RadioGroup>
                    </Form-item>
                    </Col>
                </Row>
            </Form>
        </Modal>       
    </div>
</template>
<script>
	export default {
        data () {
            return {
                /*用于查找的菜单id*/
                buildingSearch:null,
            	/*选择的数量*/
                count:null,
            	/*选中的组数据*/
                groupId:null,
            	/*新建modal的显示参数*/
                newModal:false,
                /*修改modal的显示参数*/
                modifyModal:false,
            	/*分页total属性绑定值*/
                total:0,
                /*loading*/
                loading: true,
                /*pageInfo实体*/
                pageInfo:{
                	page:0,
                	pageSize:10
                },
                /*楼栋管理数据*/
                building:{
                    id:null,
                    projectId:null,
                    villageCode:null,
                    buildingCode:null,
                    buildingName:null,
                    floorTotal:null,
                    houseTotal:null,
                    architecturalAge:null,
                    propertyRights:null,
                    buildingLevel:null,
                    hasLeft:null
                },
                /*用于添加的楼栋数据*/
                buildingAdd:{
                    id:null,
                    projectId:null,
                    villageCode:null,
                    buildingCode:null,
                    buildingName:null,
                    floorTotal:null,
                    houseTotal:null,
                    architecturalAge:null,
                    propertyRights:null,
                    buildingLevel:null,
                    hasLeft:null
                },
                /*用于修改的楼栋数据*/
                buildingModify:{
                    id:null,
                    projectId:null,
                    villageCode:null,
                    buildingCode:null,
                    buildingName:null,
                    floorTotal:null,
                    houseTotal:null,
                    architecturalAge:null,
                    propertyRights:null,
                    buildingLevel:null,
                    hasLeft:null
                },
                /*新建验证*/
                ruleNew:{
                    householdName: [
                        { type:'string',required: true, message: '输入住户名称1111', trigger: 'blur' }
                    ],
                    houseId: [
                        { type:'string',required: true, message: '选择关联房屋', trigger: 'blur' }
                    ]
                },
                /*修改验证*/
                ruleModify:{
                    householdName: [
                        { type:'string',required: true, message: '输入住户名称', trigger: 'blur' }
                    ],
                    houseId: [
                        { type:'string',required: true, message: '选择关联房屋', trigger: 'blur' }
                    ]
                },
                /*菜单列表*/
                householdList:[],
            	/*生产类型表显示字段*/
            	columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '所属小区',
                        key: 'projectName'
                    },
                    {
                        title: '项目编码',
                        key: 'villageCode'
                    },
                    {
                        title: '楼栋名称',
                        key: 'buildingName',
                    },
                    {
                        title: '楼层数',
                        key: 'floorTotal'
                    },
                    {
                        title: '户数',
                        key: 'houseTotal'
                    },
                    {
                        title: '建筑年代',
                        key: 'architecturalAge'
                    },
                    {
                        title: '产权性质',
                        key: 'propertyRights'
                    },
                    {
                        title: '房屋类型',
                        key: 'buildingLevel'
                    },
                    {
                        title: '有无电梯',
                        key: 'hasLeftNew'
                    }
                ],
                /*生产类型表数据*/
                data1: [],
                /*上传图片定义*/
                defaultList: [
                    {
                        'name': 'a42bdcc1178e62b4694c830f028db5c0',
                        'url': 'https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar'
                    },
                    {
                        'name': 'bc7521e033abdd1e92222d733590f104',
                        'url': 'https://o5wwk8baw.qnssl.com/bc7521e033abdd1e92222d733590f104/avatar'
                    }
                ],
                imgName: '',
                visible: false,
                uploadList: [],
                //存储小区选项
                allBuild:[],
                //小区id
                residenceId:null,
                //楼栋名称搜索
                buildingId:null
            }
        },
        mounted(){
        	/*页面初始化调用方法*/
            this.getTable({
                "pageInfo":this.pageInfo,
                'residenceId':'',
                'buildingId':this.buildingId
            });
            this.getAllBuilding()
        },
        methods:{
        	/*pageInfo实体初始化*/
        	initPageInfo(){
        		this.pageInfo.page = 0;
        		this.pageInfo.pageSize = 10;
        	},
            /*初始化楼栋信息*/
            initBuilding(){
                this.building.id = null,
                this.building.projectId = null,
                this.building.villageCode = null,
                this.building.buildingCode = null,
                this.building.buildingName = null,
                this.building.floorTotal = null,
                this.building.houseTotal = null,
                this.building.architecturalAge = null,
                this.building.propertyRights = null,
                this.building.buildingLevel = null,
                this.building.hasLeft = null
            },
            /*新增楼栋信息*/
            initBuildingNew(){
                this.buildingAdd.id = null,
                this.buildingAdd.projectId = null,
                this.buildingAdd.villageCode = null,
                this.buildingAdd.buildingCode = null,
                this.buildingAdd.buildingName = null,
                this.buildingAdd.floorTotal = null,
                this.buildingAdd.houseTotal = null,
                this.buildingAdd.architecturalAge = null,
                this.buildingAdd.propertyRights = null,
                this.buildingAdd.buildingLevel = null,
                this.buildingAdd.hasLeft = null
            },
            /*修改楼栋信息*/
            initBuildingModify(){
                this.buildingModify.id = null,
                this.buildingModify.projectId = null,
                this.buildingModify.villageCode = null,
                this.buildingModify.buildingCode = null,
                this.buildingModify.buildingName = null,
                this.buildingModify.floorTotal = null,
                this.buildingModify.houseTotal = null,
                this.buildingModify.architecturalAge = null,
                this.buildingModify.propertyRights = null,
                this.buildingModify.buildingLevel = null,
                this.buildingModify.hasLeft = null
            },
            /*楼栋新增设置*/
            buildingSet(e){
                this.building.id = e.id,
                this.building.projectId = e.projectId,
                this.building.villageCode = e.villageCode,
                this.building.buildingCode = e.buildingCode,
                this.building.buildingName = e.buildingName,
                this.building.floorTotal = e.floorTotal,
                this.building.houseTotal = e.houseTotal,
                this.building.architecturalAge = e.architecturalAge,
                this.building.propertyRights = e.propertyRights,
                this.building.buildingLevel = e.buildingLevel,
                this.building.hasLeft = e.hasLeft
            },
            /*楼栋新增设置*/
            buildingNewSet(e){
                this.buildingAdd.id = e.id,
                this.buildingAdd.projectId = e.projectId,
                this.buildingAdd.villageCode = e.villageCode,
                this.buildingAdd.buildingCode = e.buildingCode,
                this.buildingAdd.buildingName = e.buildingName,
                this.buildingAdd.floorTotal = e.floorTotal,
                this.buildingAdd.houseTotal = e.houseTotal,
                this.buildingAdd.architecturalAge = e.architecturalAge,
                this.buildingAdd.propertyRights = e.propertyRights,
                this.buildingAdd.buildingLevel = e.buildingLevel,
                this.buildingAdd.hasLeft = e.hasLeft
            },
            /*楼栋修改设置*/
            buildingModifySet(e){
                this.buildingModify.id = e.id,
                this.buildingModify.projectId = e.projectId,
                this.buildingModify.villageCode = e.villageCode,
                this.buildingModify.buildingCode = e.buildingCode,
                this.buildingModify.buildingName = e.buildingName,
                this.buildingModify.floorTotal = e.floorTotal,
                this.buildingModify.houseTotal = e.houseTotal,
                this.buildingModify.architecturalAge = e.architecturalAge,
                this.buildingModify.propertyRights = e.propertyRights,
                this.buildingModify.buildingLevel = e.buildingLevel,
                this.buildingModify.hasLeft = e.hasLeft
            },
            //获取到所有的小区数据
            getAllBuilding(){
                this.axios({
                    method: 'get',
                    url: '/residences/all',
                }).then(function (response) {
                    this.allBuild = response.data.data;
                }.bind(this)).catch(function (error) {
                  //alert(error);
                });
            }, 
            /*得到表数据*/
            getTable(e) {
                this.axios({
                  method: 'get',
                  url: '/buildings',
                  params: {
                    'page':this.pageInfo.page,
                    'pageSize':this.pageInfo.pageSize,
                    'residenceId':this.residenceId,
                    'buildingName':this.buildingId
                  }
                }).then(function (response) {
                    this.data1=response.data.data;
                    for(var i=0;i<this.data1.length;i++){
                        if(this.data1[i].hasLeft == '1'){
                            this.data1[i].hasLeftNew = '有'
                        }else{
                            this.data1[i].hasLeftNew = '无'
                        }
                    }    
                    this.total=response.data.totalCount;
                }.bind(this)).catch(function (error) {
                  //alert(error);
                });
            },
            /*搜索按钮点击事件*/
            search(){
                this.initPageInfo();
                this.getTable({
                    'page':this.pageInfo.page,
                    'pageSize':this.pageInfo.pageSize,
                    'residenceId':this.residenceId,
                    'buildingName':this.buildingId
                });   
            },
            /*分页点击事件*/
            pageSearch(e){
                this.pageInfo.page = e-1;
                this.getTable({  
                    'page':e.pageInfo.page,
                    'pageSize':e.pageInfo.pageSize,
                    'residenceId':this.residenceId,
                    'buildingName':this.buildingId
                });
            },
            /*新建点击触发事件*/
            openNewModal(){
                this.newModal = true;
                this.initBuildingNew();
                this.data1.sort();
                this.count = 0;
                this.groupId = null;
            },
            /*新建modal的newOk点击事件*/
            newOk (buildingAdd) {
                this.$refs[buildingAdd].validate((valid) => {
                    if (valid) {
                        this.initBuilding();
                        this.buildingSet(this.buildingAdd);
                        this.axios({
                            method: 'post',
                            url: '/buildings/building',
                            data: this.buildingAdd
                        }).then(function (response) {
                            this.initBuildingNew();
                            this.getTable({
                                "pageInfo":this.pageInfo,
                                'residenceId':'',
                                'buildingName':this.buildingName
                            });
                            this.$Message.info('新建成功');
                        }.bind(this)).catch(function (error) {
                            //alert(error);
                        });  
                        this.newModal = false;
                    }else {
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                });
            },
            /*点击修改时判断是否只选择一个*/
            openModifyModal(){
                if(this.count > 1 || this.count < 1){
                    this.modifyModal= false; 
                    this.$Message.warning('请至少选择一项(且只能选择一项)');  
                }else{
                    this.modifyModal = true;
                }
            },
            /*修改modal的modifyOk点击事件*/
            modifyOk (buildingModify) {
                this.$refs[buildingModify].validate((valid) => {
                    if (valid) {
                        this.initBuilding();
                        this.buildingSet(this.buildingModify);
                        this.axios({
                          method: 'put',
                          url: '/buildings/'+this.buildingModify.id,
                          data: this.buildingModify
                        }).then(function (response) {
                            this.initBuildingNew();
                            this.getTable({
                                "pageInfo":this.pageInfo,
                                'residenceId':'',
                                'buildingName':this.buildingName
                            });
                            this.$Message.info('修改成功');
                        }.bind(this)).catch(function (error) {
                          //alert(error);
                        });  
                        this.modifyModal = false;
                    }else {
                        this.$Message.error('表单验证失败!');
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                });
            },
            /*modal的cancel点击事件*/
            // cancel () {
            //     this.$Message.info('点击了取消');
            // },
            /*table选择后触发事件*/
            change(e){
                if(e.length==1){
                    this.buildingModifySet(e['0']);
                }
                this.setGroupId(e);              
            },
            /*通过选中的行设置groupId的值*/
            setGroupId(e){
                this.groupId=[];
                this.count=e.length;
                for (var i = 0; i <= e.length - 1; i++) {
                    this.groupId.push(e[i].id);
                }
            },
            /*删除table中选中的数据*/
            del(){
                if(this.groupId!=null && this.groupId!=""){
                    this.axios({
                      method: 'delete',
                      url: '/buildings',
                      data: this.groupId
                    }).then(function (response) {
                        this.getTable({
                            "pageInfo":this.pageInfo,
                            'residenceId':'',
                            'buildingName':this.buildingName
                        });
                        this.groupId=null;
                        this.count=0;
                        this.$Message.info('删除成功');
                    }.bind(this)).catch(function (error) {
                        //alert(error);
                    });
                }
            },
            /*表格中双击事件*/
            dblclick(e){
                this.buildingModifySet(e);
                this.modifyModal = true;
                this.data1.sort();
            },
            /*图片上传 start */
            handleView (name) {
                this.imgName = name;
                this.visible = true;
            },
            handleRemove (file) {
                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
            },
            handleSuccess (res, file) {
                file.url = 'https://o5wwk8baw.qnssl.com/7eb99afb9d5f317c912f08b5212fd69a/avatar';
                file.name = '7eb99afb9d5f317c912f08b5212fd69a';
            },
            handleFormatError (file) {
                this.$Notice.warning({
                    title: 'The file format is incorrect',
                    desc: 'File format of ' + file.name + ' is incorrect, please select jpg or png.'
                });
            },
            handleMaxSize (file) {
                this.$Notice.warning({
                    title: 'Exceeding file size limit',
                    desc: 'File  ' + file.name + ' is too large, no more than 2M.'
                });
            },
            handleBeforeUpload () {
                const check = this.uploadList.length < 5;
                if (!check) {
                    this.$Notice.warning({
                        title: 'Up to five pictures can be uploaded.'
                    });
                }
                return check;
            }
        }
    }
</script>

<style>

    .demo-upload-list{
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0,0,0,.2);
        margin-right: 4px;
    }
    .demo-upload-list img{
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }


</style>